#!/bin/bash

# 停止和禁用firewalld
echo "停止并禁用firewalld..."
systemctl stop firewalld &> /dev/null
systemctl disable firewalld &> /dev/null
if [ $? -ne 0 ]; then
echo "错误：无法停止和禁用firewalld。"
exit 1
fi
echo "完成。"

# 关闭SELinux
echo "禁用SELinux..."
setenforce 0 &> /dev/null
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config &> /dev/null
if [ $? -ne 0 ]; then
echo "错误：无法禁用SELinux。"
exit 1
fi
echo "完成。"

# 禁用swap
echo "禁用swap..."
sudo swapoff -a &> /dev/null
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab &> /dev/null
if [ $? -ne 0 ]; then
echo "错误：无法禁用swap。"
exit 1
fi
echo "完成。"

# 设置hostname和hosts
echo "设置hostname和hosts..."
sudo hostnamectl set-hostname allinone &> /dev/null
sudo sed -i '1s/^.*$/127.0.0.1 allinone/' /etc/hosts &> /dev/null
if [ $? -ne 0 ]; then
echo "错误：无法设置hostname和hosts。"
exit 1
fi
echo "完成。"

# 安装docker和依赖
echo "配置DOCKER安装源和安装DOCKER"
sudo tee /etc/yum.repos.d/docker-ce.repo <<-'EOF'
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
EOF
if [ $? -ne 0 ]; then
echo "错误：无法安装Docker。"
exit 1
fi
echo "完成。"

sudo yum -y install docker-ce-20.10.9-3.el7 &> /dev/null
if [ $? -ne 0 ]; then
echo "错误：无法安装Docker。"
exit 1
fi
echo "完成。"

# 修改docker配置文件
echo "修改Docker配置文件..."
mkdir /etc/docker &> /dev/null
cat > /etc/docker/daemon.json << EOF
{
"oom-score-adjust": -1000,
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "3"
},
"max-concurrent-downloads": 10,
"max-concurrent-uploads": 10,
"registry-mirrors": ["https://7bezldxe.mirror.aliyuncs.com"],
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF
if [ $? -ne 0 ]; then
echo "错误：无法创建和修改Docker配置文件。"
exit 1
fi
echo "完成。"

# 启用并重启docker服务
echo "启用并重启Docker服务..."
systemctl enable docker &> /dev/null
systemctl daemon-reload &> /dev/null
systemctl start docker &> /dev/null
systemctl restart docker &> /dev/null
if [ $? -ne 0 ]; then
echo "错误：无法启用和重启Docker服务。"
exit 1
fi
echo "完成。"

# 安装ipvsadm和nfs-utils
echo "安装ipvsadm和nfs-utils..."
sudo yum install -y ipvsadm &> /dev/null
if [ $? -ne 0 ]; then
echo "错误：无法安装ipvsadm。"
exit 1
fi

sudo yum install -y centos-release-gluster &> /dev/null
if [ $? -ne 0 ]; then
echo "错误：无法安装centos-release-gluster。"
exit 1
fi

sudo yum install -y nfs-utils &> /dev/null
if [ $? -ne 0 ]; then
echo "错误：无法安装nfs-utils。"
exit 1
fi
echo "完成。"

echo "安装成功。"
exit 0